package com.cloudlinkscm.base.authcenter.controller;

import com.cloudlinkscm.base.authcenter.BaseControllerSuper;
import com.cloudlinkscm.base.authcenter.model.Role;
import com.cloudlinkscm.base.authcenter.model.dto.RoleMoveDTO;
import com.cloudlinkscm.base.authcenter.model.dto.RoleSaveDTO;
import com.cloudlinkscm.base.authcenter.model.dto.TenantRoleSaveDTO;
import com.cloudlinkscm.base.authcenter.model.dto.TenantRoleUpdateDTO;
import com.cloudlinkscm.base.authcenter.service.RoleService;
import com.cloudlinkscm.loms.framework.core.pojo.RestfulApiResponse;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by sin on 2017/8/8.
 *
 * 租户：角色
 */
@RestController
public class RoleController extends BaseControllerSuper {

	@Autowired
	private RoleService roleService;

	@ApiOperation("角色：列表")
	@RequestMapping(value = "role/list", method = RequestMethod.GET)
	public RestfulApiResponse<List<Role>> list() {
		return RestfulApiResponse.success(roleService.list(currentTenantId()));
	}

	@ApiOperation("角色：保存")
	@RequestMapping(value = "role/save", method = RequestMethod.POST)
	public RestfulApiResponse<Role> save(@RequestBody TenantRoleSaveDTO saveDTO) {
		return RestfulApiResponse.success(roleService.save(saveDTO));
	}

	@ApiOperation("角色：更新")
	@RequestMapping(value = "role/update", method = RequestMethod.PUT)
	public RestfulApiResponse save(@RequestBody TenantRoleUpdateDTO updateDTO) {
		return RestfulApiResponse.success("更新成功", roleService.update(updateDTO));
	}

	@ApiOperation("角色：激活")
	@RequestMapping(value = "role/{id}/active", method = RequestMethod.PUT)
	public RestfulApiResponse active(@PathVariable("id") String id) {
		return RestfulApiResponse.success("激活成功", roleService.active(id));
	}

	@ApiOperation("角色：失效")
	@RequestMapping(value = "role/{id}/invalid", method = RequestMethod.PUT)
	public RestfulApiResponse invalid(@PathVariable("id") String id) {
		return RestfulApiResponse.success("失效成功", roleService.invalid(id));
	}


	///
	/// 角色权限 分配


	@ApiOperation("角色资源分配：移动")
	@RequestMapping(value = "role_relation/{id}/move", method = RequestMethod.PUT)
	public RestfulApiResponse roleRelationRemove(@PathVariable("id") String id,
	                                             @Validated @RequestBody RoleMoveDTO moveDTO) {
		return RestfulApiResponse.success("移动成功",
				roleService.roleRelationMove(id, currentTenantId(), moveDTO));
	}

	@ApiOperation("角色资源分配：tree")
	@RequestMapping(value = "role_relation/{id}/tree", method = RequestMethod.GET)
	public RestfulApiResponse roleRelationTree(@PathVariable("id") String id) {
		return RestfulApiResponse.success(roleService.roleRelationTree(id));
	}
}
